package DramCore
{
	import GibUI.GameScreen.GibGameScreen;
	
	import DramScript.ScriptDefinition;
	import DramScript.ScriptExecuter;
	import DramScript.ScriptLauncher;
	
	import mx.core.Application;
	
	import utils.Trace;
	import DramScript.ExecuteCommandResult;
	import flash.accessibility.Accessibility;

	public class GibGame extends ScriptLauncher
	{
		private var gameScreen:GibGameScreen;
		private var _placeArr:Array;
		
		public function GibGame(){
			super();
			this.visible = false;
			this._placeArr = new Array();
		}
		public function createaGOD( ):void { 
			var startScript:String = Application.application.startScript; 		
			Trace.myTrace("GibGame constructor: "+startScript);
			this.gameScreen = Application.application.gameScreen;
			if( !this.gameScreen ){
				Trace.throwError("GibGame Constructor no gameScreen");
			}
			this.loadDramScript( startScript );	
		}  
		override public function executeCommand(target:String, command:String, paramArr:Array):ExecuteCommandResult {
			Trace.execCommTrace("Game",target,command,paramArr);
			var ecr:ExecuteCommandResult = new ExecuteCommandResult( );
			var actor:Actor;
			switch(target.toUpperCase()){	
				case "GAME":
					switch( command.toUpperCase() ){
						case "SYS_NEWPLACE": // NEWPLACE PLACENAME BACKGROUND_GRAPHIC
							this.newPlace( paramArr[0], paramArr[1] );
						break;
						case "BACKGROUND":
//							this.gameScreen.newPlaceLayer( paramArr[0], paramArr[1], paramArr[2], paramArr[3]);
						break;
						case "LAYER": // LAYER Placename Graphicname x=0 y=0 depth = -1
							Place( this._placeArr[ this.findPlace( paramArr[0] ) ]).newPlaceLayer( paramArr[1], paramArr[2], paramArr[3], paramArr[4]);
						break;
						case "HOTSPOT_CALLSCRIPT": // LAYER Placename Graphicname scriptname x=0 y=0 depth = -1
							Place( this._placeArr[ this.findPlace( paramArr[0] ) ]).hotspotCallScript( paramArr[1], paramArr[2], paramArr[3], paramArr[4], paramArr[5]);
						break;
						case "GOPLACE":
							this.gameScreen.goPlace( this._placeArr[ this.findPlace( paramArr[0] ) ] );
						break;
						case "SYS_FINDPLACE": // Called by ScriptCompiler to validate placenames
							if( this.findPlace( paramArr[0] ) == -1) {
								ecr = new ExecuteCommandResult("FAIL");
							}
						break;
						case "SYS_NEWACTOR":
							// ACTORNAME SWFNAME DEFAULTSYMBOLNAME
							this.newActor( paramArr[0], paramArr[1], paramArr[2] );
						break;
						case "SYS_FINDACTOR": // Called by ScriptCompiler to validate placenames
							if( !this._actors[ paramArr[0] ] ) {
								ecr = new ExecuteCommandResult("FAIL");
							}
						break;
						case "ENDGAME":
						break;
						case "ENTER": // params ThingName:String [x,y,z]
							actor = this._actors[ paramArr[0] ];
							if( actor ){
								this.gameScreen.enterActor( actor, paramArr[1], paramArr[2], paramArr[3] );
							}
						break;
						default:
							ecr = super.executeCommand( target, command, paramArr );		
						break;
					}
				break;
				case "ACTOR":
					actor = this._actors[ paramArr[0] ];
					if( actor != null ){
						ecr = actor.executeCommand("actor",command,paramArr) ;
					} else {
						Trace.throwError("commandThing ERROR: No such actor as: "+paramArr[0]);
						ecr = new ExecuteCommandResult("FAIL");
					} ;
				break;
				default:
					ecr = super.executeCommand( target, command, paramArr );		
				break;

			}
			return ecr;
		}
		// Create an Actor and put it in the associative array
		private function newActor( actorName:String, swfName:String, defaultSymbolName:String  ):void {
				// Replace any existing actor
				this._actors[ actorName ] = new Actor( actorName, swfName, defaultSymbolName );
		}
		// Create a Place and put it in an array
		private function newPlace( placeName:String, background:String  ):void {
			var placeIndex:int = this.findPlace( placeName ) ;
			if( placeIndex >= 0){
				// Replace the existing place
				this._placeArr[ placeIndex ] = new Place( placeName, background );
			} else {
				// Push a new Place onto the array
				this._placeArr.push( new Place( placeName, background ) );
			}
		}
		// Checks if place exists and returns array index or -1 if it doesn't
		private function findPlace( placeName:String ):int {
			for( var i:int = 0; i < this._placeArr.length; i++){
				if( this._placeArr[i].name == placeName )
					return i;
			}
			return -1;
		}
	}
}